<?php

namespace App\Kuafu\Shop\Validate;

use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\Shop\Model\PrinterModel;
use App\Kuafu\Base\Enum\DBEnum;
use App\Kuafu\ProductMenu\Model\ProductMenu;
use App\Kuafu\Shop\Model\Shop;
use Illuminate\Http\Request;

class PrinterValidate extends BaseValidate
{
    public static function printerById($data)
    {
        $rules = [
            'id' => [
                'required',
                'integer',
                'min:1'
            ],
        ];

        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function addPrinter($data)
    {
        $rules = [
            'name'        => ['required'],
            'printerType' => ['required', 'integer', 'between:0,2'],
            'deviceType'  => ['integer', 'between:0,4'],
            'printerIp'   => ['required',
                function ($attribute, $value, $fail) use ($data) {
                    $printerIp = PrinterModel::query()->where(['printerIp' => $data['printerIp'], 'shopId' => $data['shopId'], 'brandId' => $data['brandId']])->first();
                    if($printerIp == null){
                        return true;
                    }else{
                        $fail("该打印机IP已被使用");
                    }
                }],
            'status'      => ['integer', 'between:0,4'],
            'deviceId'    => ['integer', 'min:0',
                function ($attribute, $value, $fail) use ($data) {
                    $deviceId = PrinterModel::query()->where(['deviceId' => $data['deviceId'], 'shopId' => $data['shopId'], 'brandId' => $data['brandId']])->first();
                    if($deviceId == null){
                        return true;
                    }else{
                        $fail("该打印机配置ID已被使用");
                    }
                }],
            'printerPort' => ['integer', 'min:0'],
        ];

        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function setPrinter($data)
    {
        $rules = [
            'printerType' => ['integer', 'between:0,2'],
            'deviceId'  => [
                function ($attribute, $value, $fail) use ($data) {
                    $deviceId = PrinterModel::query()->where(['deviceId' => $data['deviceId'], 'shopId' => $data['shopId'], 'brandId' => $data['brandId']])->first();
                    if($deviceId == null){
                        return true;
                    }else{
                        if ($deviceId->id != $data['id']) {
                            $fail("该打印机配置ID地址已存在");
                        }
                    }
                }
            ],
            'id'          => ['required', 'integer', 'min:0'],
            'printerIp'   => [
                function ($attribute, $value, $fail) use ($data) {
                    $printerIp = PrinterModel::query()->where(['printerIp' => $data['printerIp'], 'shopId' => $data['shopId'], 'brandId' => $data['brandId']])->first();
                    if ($printerIp == null) {
                        return true;
                    }else{
                        if ($printerIp->id != $data['id']) {
                            $fail("该打印机IP地址已存在");
                        }
                    }
                }
            ],
            'printerPort' => ['integer'],
            'deviceType'  => ['integer', 'between:0,4'],
            'status'      => ['integer', 'between:0,4'],
        ];

        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }


}
